﻿using System;
using IOCDemo.Domain;
using IOCDemo.Interfaces;

namespace IOCDemo.Services
{
    public class MockUserService : IUserService
    {
        private readonly ICacheService cacheService;
        private readonly ISettingsService settingsService;
        private readonly ILoggingService loggingService;

        public MockUserService(ILoggingService loggingService, ICacheService cacheService, ISettingsService settingsService)
        {
            this.loggingService = loggingService;
            this.cacheService = cacheService;
            this.settingsService = settingsService;
        }

        #region Implementation of IUserService

        public AuthToken AuthenticateUser(string username, string password)
        {
            loggingService.Info("Trying to authenticate user with username: " + username);

            return new AuthToken
                       {
                           OrganizationKey = settingsService.GetSetting<string>("OrganizationKey"),
                           SessionKey = Guid.NewGuid().ToString(),
                           User = new User
                                      {
                                          UserId = "johnSmith1970",
                                          FirstName = "John",
                                          LastName = "Smith"
                                      }
                       };
        }

        public User GetUser(AuthToken authToken)
        {
            loggingService.Info(string.Format("Getting user id: {0}, with organization key: {1}", authToken.User.UserId, authToken.OrganizationKey));

            string cacheKey = string.Format("{0}_User", authToken.SessionKey);

            if (!cacheService.Contains(cacheKey))
                cacheService.Put(cacheKey, new User
                                               {
                                                   UserId = "johnSmith1970",
                                                   FirstName = "John",
                                                   LastName = "Smith"
                                               });

            return cacheService.Get(cacheKey) as User;
        }

        #endregion
    }
}